package com.example.menuvirtualclient;

import java.util.List;

import com.example.menuvirtualclient.core.Menu;
import com.example.menuvirtualclient.core.Persona;
import com.example.menuvirtualclient.dto.OrderDto;
import com.example.menuvirtualclient.presenter.MenuPresenter;
import com.example.menuvirtualclient.repository.MenuRepository;
import com.example.menuvirtualclient.rule.MenuRule;
import com.example.menuvirtualclient.util.RestClient;
import com.example.menuvirtualclient.util.RestClient.RequestMethod;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WsMenuClient extends AsyncTask<String, Integer, Double>{

    private RestClient client;
	private String request;
	private List<Menu> menus;
	private MenuRule rule;
	private MenuPresenter presenter;
	
	private boolean error = false;
    private Context mContext;
    private ProgressBar pb;
	
	public WsMenuClient(Context context, ProgressBar pb, MenuPresenter presenter){
		this.mContext = context;
		this.pb = pb;
		this.presenter = presenter;
	}
	
	public WsMenuClient(MenuRule rule, Context context, ProgressBar pb, MenuPresenter presenter){
		this.rule = rule;
		this.mContext = context;
		this.pb = pb;
		this.presenter = presenter;
	}
	
	public List<Menu> getMenus(){
		return menus;
	}
	
	public String getRequest(){
		return request;
	}
	
	protected void onPreExecute() {
		
	}
	
	@Override
	protected Double doInBackground(String... params) {
		client = new RestClient(params[0]);
		String response = "Error";
		
		try {
			client.Execute(RequestMethod.GET);
			response = client.getResponse()
					.replace("<ns:getMenusJsonResponse xmlns:ns=\"http://ws.apache.org/axis2\"><ns:return>", "")
					.replace("</ns:return></ns:getMenusJsonResponse>", "");
			Gson gson = new Gson();
			List<Menu> list = gson.fromJson(response, new TypeToken<List<Menu>>(){}.getType());
		    if(list != null && list.size() > 0){
		    	menus = list;
		    	response = "Ok";
		    	for(Menu menu : menus){
		    		Menu entity = presenter.getMenu(menu.getId());
		    		if(entity != null){
		    			if(!entity.equals(menu)){
		    				presenter.Update(menu);
		    			}		    			   				
		    		}
		    		else {
		    			presenter.Insert(menu);
	    			}
		    	}
		    	presenter.LoadMainMenu();
		    }
		    else{		    	
		    	Log.i("WsMenuClient", "null");
		    	error = true;
		    }
			//publishProgress(response);
		} catch (Exception e) {
			e.printStackTrace();
			error = true;
			cancel(true);
		}
		pb.setVisibility(View.GONE);
		Toast.makeText(mContext, "command sent", Toast.LENGTH_LONG).show();
		return null;
	}

	protected void onProgressUpdate(Integer... progress) {
		pb.setProgress(progress[0]);
		Log.i("Info", "Procesando...");
	}

	protected void onPostExecute(String content) {
		pb.setVisibility(View.GONE);
		Toast.makeText(mContext, "command sent", Toast.LENGTH_LONG).show();
	}	
	
	
}
